from typing import List
import os
import zipfile
import time
import aiofiles
from .const import PATH_ROBOT_INSTANCE,PATH_ROBOT_ZIP_TEMP

def biz_response(code:int,msg:str):
    return {
        "code":code,
        "msg":msg
    }

def get_timestamp():
    return int(round(time.time() * 1000))




def zip_files(files:List[str],file_name:str=None)->str:
    """
    :param files: 待压缩文件列表
    :return: 压缩后的文件,格式 xxx.zip
    """
    if not file_name:
        stamp = get_timestamp()
        file_name = os.path.join(PATH_ROBOT_ZIP_TEMP,f"temp_{stamp}.zip")
    if not os.path.exists(PATH_ROBOT_ZIP_TEMP):
        os.mkdir(PATH_ROBOT_ZIP_TEMP)
    with zipfile.ZipFile(file_name,"w") as zipf:
        for f in files:
            zipf.write(f)
        return zipf.filename







async def match_content_in_file_async(filename: str,content: str,encoding: str = "gbk") -> bool:
    """
    查找文件中是否存在给定内容
    目前发现这种方式很慢，暂不使用
    :param filename:
    :param content:
    :param encoding:
    :return:
    """
    async with aiofiles.open(filename,mode="r",encoding=encoding) as f:
        async for line in f:
            if content in line:
                return True